using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
using PriorityQueue;

namespace MUSA
{
    class Projectile : Object
    {
        #region Member Variables

        private BoundingSphere projectileBoundingSphere;

        private Vector3 direction;

        private float speed;

        public float lifeSpan;

        public ushort projectileID;

        #endregion

        #region Constructor

        /// <summary>
        /// Default Constructor
        /// </summary>
        Projectile(Game game)
            : base(game)
        {
            IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)mGame.Services.GetService(typeof(IGraphicsDeviceService));

            position = Vector3.Zero;
            direction = Vector3.Zero;
            lifeSpan = 0;
        }

        /// <summary>
        /// Primary Constructor
        /// </summary>
        /// <param name="_pos">starting position</param>
        /// <param name="_dir">direction</param>
        /// <param name="_sd">speed</param>
        /// <param name="ID">ID number of tank which spawned it</param>
        public Projectile(Game game, Vector3 _pos, Vector3 _dir, ushort ID)
            : base(game)
        {
            projectileID = ID;

            position = _pos;
            direction = _dir;
            speed = 20;
            lifeSpan = 0;

            objectModel = new MModel("Media\\projectile", position, new Vector3(1, 0, 0));

            changeColor(Color.Green);

            IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)mGame.Services.GetService(typeof(IGraphicsDeviceService));

            //copy bounding sphere from projectile model
            foreach (ModelMesh m in objectModel.MODEL.Meshes)
            {
                projectileBoundingSphere = m.BoundingSphere;
                projectileBoundingSphere = new BoundingSphere(m.BoundingSphere.Center , m.BoundingSphere.Radius);
                projectileBoundingSphere.Center += position;
            }
        }

        #endregion

        #region Member Methods

        public void changeColor(Color newColor)
        {
            objectModel.COLOR = newColor;
        }

        public override bool Collide(BoundingBox pBox)
        {
            return projectileBoundingSphere.Intersects(pBox);
        }

        public override bool Collide(BoundingSphere pSphere)
        {
            return projectileBoundingSphere.Intersects(pSphere);
        }

        public BoundingSphere getBounding()
        {
            return projectileBoundingSphere;
        }

        public override void Update(GameTime gametime)
        {
            //update position
            position = position + (direction * (float)gametime.ElapsedGameTime.TotalSeconds * speed);
            objectModel.POSITION = position;
            objectModel.Update(gametime);

            projectileBoundingSphere.Center = position;

            //update life span
            lifeSpan += (float)gametime.ElapsedGameTime.TotalSeconds;

            base.Update(gametime);
        }

        protected override void Create()
        {
        }

        public override void Draw(GameTime gTime)
        {
            objectModel.Draw();
        }

        #endregion
    }
}
